import Vue from 'vue';
import Vuex from 'vuex';
import $mConstDataConfig from '@/config/constData.config';
import $mSettingConfig from '@/config/setting.config';

Vue.use(Vuex);
const token = uni.getStorageSync('token') || '';
const USER = uni.getStorageSync('user') || {};
const THEMECOLOR = uni.getStorageSync('themeColor');
const store = new Vuex.Store({
	state: {
		// 用户token
		token: token,
		// 用户信息
		userInfo: USER.member,
		themeColor: THEMECOLOR
	},
	getters: {
		// 全局配置
		themeColor: state => {
			let theme = state.themeColor;
			if (!theme) {
				theme = $mConstDataConfig.themeList.filter((item) => item.name === ($mSettingConfig.styleType || 'rf'))[0];
			}
			return theme;
		},
		// 判断用户是否登录
		hasLogin: state => {
			return !!state.token;
		}
	},
	mutations: {
		login(state, provider) {
			state.token = provider.token;
			state.user = provider;
			uni.setStorageSync('user', provider);
			uni.setStorageSync('token', provider.token);
		},
		logout(state) {
			state.token = '';
			uni.removeStorageSync('token');
			uni.removeStorageSync('user');
			uni.reLaunch({
				url: "/pages/public/login"
			});
		}
	},
	actions: {
		logout({ commit }) {
			commit('logout');
		}
	}
});

export default store;
